package ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.ck.hzq.PaymentorderActivity;
import net.ck.hzq.R;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import Bean.OrderBean;
import base.LazyFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.CancelOrderEvent;
import event.NetErrorEvent;
import event.PayOrderEvent;
import ui.activity.order.biz.OrderBiz;
import ui.activity.order.component.DaggerOrderComponent;
import ui.activity.order.contract.OrderContract;
import ui.activity.order.module.OrderModule;
import ui.activity.order.presenter.OrderPresenter;
import ui.adapter.OrderWaitAdapter;
import util.AlertUtils;
import widget.alertview.AlertView;

/**
 * Created by chenjian on 18/2/21.
 * <p>
 * 订单待支付
 */

public class OrderWaitFra extends LazyFragment implements OrderContract.View {


    @Inject
    OrderPresenter presenter;
    @Inject
    OrderBiz biz;


    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    OrderWaitAdapter adp;

    private boolean isPrepared;// 界面否
    private boolean isALeady = true;
    private SharedPreferences sp;

    AlertDialog updateDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.pull_refresh_layout, container, false);
        ButterKnife.bind(this, view);
        DaggerOrderComponent.builder().orderModule(new OrderModule(this)).build().inject(this);
        presenter.setBiz(biz);
        sp = getActivity().getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {

            }
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getOrderList(true, sp.getString("id", null), "1", "1");
            }
        });
//        isPrepared = true;
//        ptr.autoRefresh(true);
        return view;
    }

    @Override
    protected void lazyLoad() {
        if (isPrepared && isVisible && isALeady) {
            // TODO 在这里处理逻辑
            isALeady = false;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registerPayEvent(PayOrderEvent event) {
        Intent intent = new Intent(getContext(), PaymentorderActivity.class);
        intent.putExtra("id", event.id + "");
        intent.putExtra("price", event.price + "");
        startActivityForResult(intent, 11);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registerCancelOrder(CancelOrderEvent event) {
        AlertView checkAlert = AlertUtils.showInfo(getActivity(), new AlertUtils.AlertViewControl() {
            @Override
            public void onItemClickListener(int positions) {
                if (positions == 0) {
                    presenter.cancleOrder(event.orderId, sp.getString("id", null), "1", "1");
                }
            }
        }, "提示", "是否取消订单？");
        checkAlert.show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.bind(getActivity()).unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.getOrderList(true, sp.getString("id", null), "1", "1");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }


    @Override
    public void upDateUI(List<OrderBean.DataBean.ItemsBean> bean) {
        if (adp == null) {
            adp = new OrderWaitAdapter(getActivity(), bean);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }


}
